<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class TypeChannel_Model extends CI_Model{
	protected $_name = "type_channel";
	protected $_primary = "TcId";
	var $Name_vn = "Name_vn", $Name_en = "Name_en", $Description_vn = "Description_vn", $Description_en = "Description_en", $Status = "Status";
	
	function getAllTypeChannel(){
		$this->db->where($this->Status, 1);
		$this->db->order_by($this->_primary, "ASC");
		$query = $this->db->get($this->_name);
		return $query->result();
	}
	
	function countChannelByTcId($TcId){
		$this->db->where($this->_primary, $TcId);
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get("channel");
		return $query->num_rows();
	}
	
	function getTypeChannelByTcId($TcId){
		$this->db->where($this->_primary, $TcId);
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	
	function getTypeChannel(){
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->NumberChannel = $this->countChannelByTcId($data[$j]->TcId);
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function countTypeChannel(){
		$this->db->where($this->Status, 1);
		$this->db->or_where($this->Status, 2);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	
	function searchTypeChannel($keysearch){
		$this->db->like($this->Name_vn, $keysearch);	
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->NumberChannel = $this->countChannelByTcId($data[$j]->TcId);
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function changeStatusTypeChannel($TcId, $Status){
		try{
			$this->db->where($this->_primary, $TcId);
			if($Status == 1){
				$Status = 2;
			}elseif($Status == 2){
				$Status = 1;
			}
			$this->db->update($this->_name, array($this->Status => $Status));
			return $Status;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function deleteTypeChannel($TcId){
		try{
			$this->db->where($this->_primary, $TcId);
			$this->db->update($this->_name, array($this->Status => 3));
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function updateTypeChannel($TcId){
		try{
			$this->db->where($this->_primary, $TcId);
			$data = array(
				$this->Name_vn => trim($this->input->post($this->Name_vn, true)),
				$this->Name_en => trim($this->input->post($this->Name_en, true)),
				$this->Description_vn => trim($this->input->post($this->Description_vn)),
				$this->Description_en => trim($this->input->post($this->Description_en))
			);
			$this->db->update($this->_name, $data);
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function insertTypeChannel(){
		try{
			$this->db->set($this->Name_vn, trim($this->input->post($this->Name_vn, true)));
			$this->db->set($this->Name_en, trim($this->input->post($this->Name_en, true)));
			$this->db->set($this->Description_vn, trim($this->input->post($this->Description_vn)));
			$this->db->set($this->Description_en, trim($this->input->post($this->Description_en)));
			$this->db->set($this->Status, 1);
			$this->db->insert($this->_name);
			return true;
		}catch(Exception $e){
			return false;
		}
	}
}